﻿/**************************************************************

项目名称 :ThinkWide数据访问基础库
版权所有 (c) 2010 ThinkWide
+-------------------------------------------------------------
项目: 


版本: 1


创建日期:2010-8-3


作者: 章称

+-------------------------------------------------------------
描述:z

更新:   2010-8-3

ToDo:

***************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MF.Data.Manager;
using System.Data;
using MF.Data.SupplyInterface;

namespace MF.Data.WCF
{
    // NOTE: If you change the class name "WCFService" here, you must also update the reference to "WCFService" in App.config.
    public class WCFService : IWCFService
    {
        public DataSet GetDataSet(string sql)
        {
            MFDataManager dm = new MFDataManager();

            DataSet ds = dm.GetDataSet(sql);
            return ds;
        }

        public int UpdataDataSet(DataSet dataSet)
        {
            try
            {
                MFDataManager dm = new MFDataManager();

                dm.UpdateTable(dataSet.Tables[0], dataSet.Tables[0].TableName);
                return 0;
            }
            catch
            {
                return -1;
            }
        }


        public MFDBResuleInfo ExecuteSQL(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            MFDBResuleInfo info = new MFDBResuleInfo();
            MFDataManager dm = new MFDataManager();
            try
            {

                IDbCommand cmd = dm.GetCommand(true);
                cmd.CommandType = commandType;
                cmd.CommandText = sql;
                if (dataParameters!=null)
                foreach (MFDataParameter param in dataParameters)
                {

                    IDataParameter dp = dm.GetParameter(param.ParameterName, param.DbType, param.Size);
                    param.Assign(dp);

                    cmd.Parameters.Add(dp);
                }

                cmd.Prepare();

                info.Count = cmd.ExecuteNonQuery();

                if (dataParameters != null)
                foreach (IDataParameter param in cmd.Parameters)
                {
                    if (param.Direction != ParameterDirection.Input)
                        dataParameters[param.ParameterName].Value = param.Value;
                }
                info.DataParameters = dataParameters;
                info.IsSuccess = true;
                info.Code = 0;
                dm.Close();
                return info;
            }
            catch (Exception ex)
            {
                info.IsSuccess = false;
                info.Message = ex.Message;
                info.Count = -1;
                dm.Close();
                return info;
            }
        }

        public MFDBResuleDataSetInfo ExecuteSQLDataSet(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            MFDBResuleDataSetInfo info = new MFDBResuleDataSetInfo();
            MFDataManager dm = new MFDataManager();
            try
            {

                IDbCommand cmd = dm.GetCommand(true);
                cmd.CommandType = commandType;
                cmd.CommandText = sql;

                if (dataParameters != null)
                foreach (MFDataParameter param in dataParameters)
                {

                    IDataParameter dp = dm.GetParameter(param.ParameterName, param.DbType, param.Size);
                    param.Assign(dp);

                    cmd.Parameters.Add(dp);
                }

                IDataAdapter da = dm.GetDataAdapter(cmd);

                cmd.Prepare();

                DataSet ds = new DataSet();
                dm.CheckedOpen();
                da.Fill(ds);
                if (ds.Tables.Count > 0)
                    info.Count = ds.Tables[0].Rows.Count;
                else
                    info.Count = 0;

                if (dataParameters != null)
                foreach (IDataParameter param in cmd.Parameters)
                {
                    if (param.Direction != ParameterDirection.Input)
                        dataParameters[param.ParameterName].Value = param.Value;
                }

                info.DataParameters = dataParameters;
                info.ResultDataSet = ds;
                info.IsSuccess = true;
                info.Code = 0;
                dm.Close();
                return info;
            }
            catch (Exception ex)
            {
                info.IsSuccess = false;
                info.Message = ex.Message;
                info.Count = -1;
                dm.Close();
                return info;
            }
        }

        public MFWcfInformatiom GetServiceInformatiom()
        {
            MFWcfInformatiom msi = new MFWcfInformatiom();
            msi.Version = "0.1";
            msi.ServerType = "wcf";
            msi.ServerTime = DateTime.Now;
            return msi;
        }


    }
}
